package com.myblog.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.myblog.domain.entity.enums.GlobalResponseEnum;
import lombok.Getter;

import java.io.Serializable;
@JsonInclude(JsonInclude.Include.NON_NULL)//为NULL的字段不序列化
@Getter
//实现Serializable接口表示该类可被序列化
public class GlobalResponse<T> implements Serializable  {
    private Integer code;
    private String msg;
    private T data;
    //只需返回msg的接口
    public  GlobalResponse(GlobalResponseEnum globalResponseEnum){
        this.code=globalResponseEnum.code;
        this.msg=globalResponseEnum.msg;

    }
    //需要返回整体带数据的msg接口
    public GlobalResponse(GlobalResponseEnum globalResponseEnum,T data){
        this.code=globalResponseEnum.code;
        this.msg=globalResponseEnum.msg;
        this.data=data;
    }
    public  GlobalResponse(Integer code,String msg){
        this.code=code;
        this.msg=msg;
    }
    //GlobalResponse.ok()(直接返回正确信息)
    public static GlobalResponse  ok(){
        GlobalResponse globalResponse=new GlobalResponse(GlobalResponseEnum.SUCCESS);
        return globalResponse;
    }
    //GlobalResponse.ok(data)传数据进去的正确信息
    public static GlobalResponse  ok(Object data){
        GlobalResponse globalResponse=new GlobalResponse(GlobalResponseEnum.SUCCESS,data);
        return globalResponse;
    }

    //GlobalResponse.error()(直接返回错误信息)
    public static GlobalResponse  error(){
        GlobalResponse globalResponse=new GlobalResponse(GlobalResponseEnum.ERROR);
        return globalResponse;
    }
    public static GlobalResponse  error(GlobalResponseEnum globalResponseEnum){
        GlobalResponse globalResponse=new GlobalResponse(globalResponseEnum);
        return globalResponse;
    }
    public static GlobalResponse  error(Integer code,String msg){
        GlobalResponse globalResponse=new GlobalResponse(code,msg);
        return globalResponse;
    }

    //额外添加数据的接口
    public  GlobalResponse<?> setData(T data){
        this.data=data;
        return this;
    }

    public GlobalResponse setCode(Integer code) {
        this.code = code;
        return this;
    }

    public GlobalResponse setMsg(String msg) {
        this.msg = msg;
        return this;
    }


    public GlobalResponse<?> errorT(GlobalResponseEnum globalResponseEnum) {
        this.setCode(GlobalResponseEnum.ERROR.code);
        this.setMsg(GlobalResponseEnum.ERROR.msg);
        return this;
    }


    public GlobalResponse<?> okT(GlobalResponseEnum globalResponseEnum,T data) {
        this.setCode(GlobalResponseEnum.ERROR.code);
        this.setMsg(GlobalResponseEnum.ERROR.msg);
        this.setData(data);
        return this;
    }

}
